var express = require('express');
var async = require("async");
var router = express.Router();
var tools = require("../utils/tools.js");

/* GET home page. */
router.get('/', function(req, res, next) {
    var pagename = req.query.pagename;
    res.render('spa', { pagename: pagename });
});
router.post("/save", function(req, res, next) {
    var data = req.body;
    console.log(data)
    var appinfo = {
        appname: data.appname,
        tablename: data.tablename,
        createDate: new Date()
    }
    data.createDate = new Date();
    var dataModel = null;
    var appinfoModel = null;
    if (data.appname && data.tablename) {
        dataModel = require("../model/spa_" + data.appname + "_" + data.tablename + ".js");
        appinfoModel = require("../model/spa.js");
    }
    async.waterfall([
        function(cb) {
            new appinfoModel(appinfo).save(function(err) {
                if (err) {
                    cb(new Error(err));
                } else {
                    cb(null);
                }
            });
        },
        function(cb) {
            new dataModel(data).save(function(err) {
                if (err) {
                    cb(new Error(err));
                } else {
                    cb(null)
                }
            });
        }
    ], function(err, result) {
        console.log(err)
        if (err) {
            res.send(500, err)
        } else {
            res.send(200,result);
        }
    });

})
router.get("/getAppInfo", function(req, res, next) {
    var appInfoModel = require("../model/spa.js");
    async.waterfall([
        function(cb) {
            appInfoModel.find({}, { __v: 0, _id: 0 }, function(err, result) {
                if (err) {
                    cb(new Error(err));
                } else {
                    cb(null, result);
                }
            })
        },
        function(result, cb) {
            var appInfo = {
                appname: [],
                tablename: []
            }
            result.map(function(data, index) {
                appInfo.appname.push(data.appname);
                appInfo.tablename.push(data.tablename);
            });
            console.log(appInfo.appname);
            appInfo.appname = appInfo.appname.distinct();
            appInfo.tablename = appInfo.tablename.distinct();
            cb(null, appInfo);
        },


    ], function(err, result) {
        if (err) {
            res.send(500, err);
        } else {
            res.send(200, result);
        }
    });
});
router.post("/getInfoByTableName", function(req, res, next) {
    var data = req.body;
    var Model = null;
    if (data) {
        Model = require("../model/spa_" + data.appname + "_" + data.tablename + ".js");
    }
    Model.find({}, { __v: 0, _id: 0 }, function(err, result) {
        if (err) {
            res.send(500, err);
        } else {
            res.send(200, result);
        }
    });
});
router.post("/email", function(req, res, next) {
    var data = req.body;
});

module.exports = router;
